﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace _1065093_1065130_1065120_DoAn
{
    public partial class QuyDinhKhuyenMai : UserControl
    {
        public QuyDinhKhuyenMai()
        {
            InitializeComponent();
        }

        public void LoadKM()
        {
            THAMSOBUS tsBUS = new THAMSOBUS();
            DataTable dt = new DataTable();
            dt = tsBUS.LoadKhuyenMai();
            dgvkhuyenmai.DataSource = dt;
        }

        private void QuyDinhKhuyenMai_Load(object sender, EventArgs e)
        {
            LoadKM();
        }

        private void dgvkhuyenmai_SelectionChanged(object sender, EventArgs e)
        {
            lbmakm.Text = dgvkhuyenmai.CurrentRow.Cells[0].Value.ToString();
            txttenkm.Text = dgvkhuyenmai.CurrentRow.Cells[1].Value.ToString();
            txtgiatri.Text = dgvkhuyenmai.CurrentRow.Cells[2].Value.ToString();
            txtkieu.Text = dgvkhuyenmai.CurrentRow.Cells[3].Value.ToString();
            dtpngaybd.Text = dgvkhuyenmai.CurrentRow.Cells[4].Value.ToString();
            dtpngaykt.Text = dgvkhuyenmai.CurrentRow.Cells[5].Value.ToString();
        }

        private void txtgiatri_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsNumber(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            try
            {
                string tenkm = txttenkm.Text;
                int giatri = Convert.ToInt32(txtgiatri.Text);
                string kieu = txtkieu.Text;
                DateTime ngaybd = Convert.ToDateTime(dtpngaybd.Text);
                DateTime ngaykt = Convert.ToDateTime(dtpngaykt.Text);

                THAMSOBUS tsBus = new THAMSOBUS();
                THAMSODTO tsDto = new THAMSODTO(-1,tenkm,giatri,kieu,ngaybd,ngaykt);
                tsBus.ThemKhuyenMai(tsDto);
                LoadKM();
                MessageBox.Show("Thành công");
                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Bạn có muốn xoá?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                try
                {
                    int ma = Convert.ToInt32(dgvkhuyenmai.CurrentRow.Cells[0].Value);

                    THAMSOBUS tsBus = new THAMSOBUS();
                    tsBus.XoaKhuyenMai(ma);
                    LoadKM();
                    MessageBox.Show("Thành công");
                    return;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Bạn có muốn sửa?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                try
                {
                    int ma = Convert.ToInt32(lbmakm.Text);
                    string tenkm = txttenkm.Text;
                    int giatri = Convert.ToInt32(txtgiatri.Text);
                    string kieu = txtkieu.Text;
                    DateTime ngaybd = Convert.ToDateTime(dtpngaybd.Text);
                    DateTime ngaykt = Convert.ToDateTime(dtpngaykt.Text);

                    THAMSOBUS tsBus = new THAMSOBUS();
                    THAMSODTO tsDto = new THAMSODTO(ma,tenkm,giatri,kieu,ngaybd,ngaykt);
                    tsBus.SuaKhuyenMai(tsDto);
                    LoadKM();
                    MessageBox.Show("Thành công");
                    return;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
        }
    }
}
